/**
\ * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
package com.test.v7;



import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.app.R;

/**
 * @author Administrator
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2015年3月10日 上午10:15:44
 * @Title <pre>加载数据 管理器</pre>
 * @Description <pre>todo</pre>
 */
public class LoadingView extends LinearLayout{
	//private LinearLayout mLayout;
	private TextView tvMsg;
	private ProgressBar barPro;
	private Button btnError;
	private Context context;
	
	public LoadingView(Context context){
		this(context,null);
	}
	
	public LoadingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		create();
	}

	private void create(){
		//mLayout=new LinearLayout(context);
		this.context=getContext();
		setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		setOrientation(VERTICAL);
		setGravity(Gravity.CENTER);
		setBackgroundColor(Color.WHITE);
		
		View v=View.inflate(context, R.layout.loadingview2, this);
		barPro=(ProgressBar) v.findViewById(R.id.progressBar1);
		btnError=(Button) v.findViewById(R.id.button1);
		tvMsg=(TextView)v.findViewById(R.id.textView1);
		
		
		//barPro=new ProgressBar(context, null,android.R.attr.progressBarStyle);
		//barPro.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		//addView(barPro,0);
		
		/*btnError=new Button(context,null,R.style.LoadButtonStyle);
		btnError.setTextColor(Color.BLACK);
		btnError.setVisibility(View.GONE);
		//btnError.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		//addView(btnError,1);
				
		tvMsg=new TextView(context);
		tvMsg.setTextColor(Color.parseColor("#000000"));
		tvMsg.setTextSize(18,TypedValue.COMPLEX_UNIT_SP);
		tvMsg.setGravity(Gravity.CENTER);
		tvMsg.setText("TEst");
		tvMsg.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
		addView(tvMsg);*/
	}
	
	public void show(){
		btnError.setVisibility(View.GONE);
		barPro.setVisibility(View.VISIBLE);
		tvMsg.setVisibility(View.VISIBLE);
		this.setVisibility(View.VISIBLE);
	}
	
	public void dismiss(){
		setVisibility(View.GONE);
	}
	
	/**
	 * @Description <pre>提示内容</pre>
	 * @param msg 消息体
	 * @return Builder
	 */
	public void setMessage(CharSequence msg){
		tvMsg.setText(msg);
	}
	
	/**
	 * @Description <pre>提示内容</pre>
	 * @param msgResid
	 * @return Builder
	 */
	public void setMessage(int msgResid){
		setMessage(context.getText(msgResid));
	}

	public boolean isShowing() {
		return getVisibility()==View.VISIBLE;
	}

	public void showError(String text){
		barPro.setVisibility(View.GONE);
		btnError.setVisibility(View.GONE);
		tvMsg.setText(text);
		setVisibility(View.VISIBLE);
	}
	
	public void onDestroyView(){
		this.removeAllViews();
		tvMsg=null;
		barPro=null;
		btnError=null;
	}
}
